package com.novel.controller;

import com.novel.novelSearch.Novel;
import com.novel.service.NovelService;
import com.search.bturls.Bturls;
import com.search.commons.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yijy on 2017/7/31.
 */
@Controller
@RequestMapping("/novel")
public class NovelController {
    @Autowired
    NovelService novelService;

    @RequestMapping("/main")
    public ModelAndView main(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("novel/novelmain");
        return modelAndView;
    }

    @RequestMapping("/search")
    public ModelAndView goSearch(String searchValue)
    {
        ModelAndView modelAndView = new ModelAndView();
        CommonPage<Novel> str = novelService.getBtDataList(searchValue,1);
        List list = str.getDataList();
        modelAndView.addObject("resultList",list);
        modelAndView.addObject("searchValue",searchValue);
        modelAndView.addObject("pageCount",str.getPageCount());
        modelAndView.addObject("currentCount",str.getCurrentCount());
        modelAndView.setViewName("novel/novelresult");
        return modelAndView;
    }

    @RequestMapping("goMagnet")
    public ModelAndView goMagnet(String url)
    {
        ModelAndView modelAndView = new ModelAndView();
        Map str = novelService.goManget(url);
        if(str.containsKey("content")){
            modelAndView.addObject("content",str.get("content"));
        }
        if(str.containsKey("title")){
            modelAndView.addObject("title",str.get("title"));
        }
        if(str.containsKey("lastChapter")){
            modelAndView.addObject("lastChapter",str.get("lastChapter"));
        }
        if(str.containsKey("mulu")){
            modelAndView.addObject("mulu",str.get("mulu"));
        }
        if(str.containsKey("nextChapter")){
            modelAndView.addObject("nextChapter",str.get("nextChapter"));
        }
        modelAndView.setViewName("novel/novelText");
        return modelAndView;
    }


    @RequestMapping("allContents")
    public ModelAndView readAllContents(String url)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Novel> str = novelService.readAllContents(url);
        modelAndView.addObject("content",str);
        modelAndView.setViewName("novel/novelCatalog");
        return modelAndView;
    }

    @RequestMapping("turnPage")
    public ModelAndView turnPage(String searchValue,String currentCount)
    {
        ModelAndView modelAndView = new ModelAndView();
        int count = Integer.parseInt(currentCount);
        CommonPage<Novel> str = novelService.getBtDataList(searchValue,count);
        List list = str.getDataList();
        modelAndView.addObject("resultList",list);
        modelAndView.addObject("searchValue",searchValue);
        modelAndView.addObject("pageCount",str.getPageCount());
        modelAndView.addObject("currentCount",str.getCurrentCount());
        modelAndView.setViewName("novel/novelresult");
        return modelAndView;
    }

}
